package com.zs.bean;

import com.zs.annotation.MyServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
// BeanDefinitionRegistryPostProcessor  和bd相关操作
//PriorityOrdered Ordered 排序类，PriorityOrdered权重优先
@Component
public class BeanPro implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, Ordered {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        //获取bean name
        String[] beanDefinitionNames = registry.getBeanDefinitionNames();
        System.out.println(beanDefinitionNames.length);
        for (String beanDefinitionName : beanDefinitionNames) {
            //获取beanDefinition
            BeanDefinition beanDefinition = registry.getBeanDefinition(beanDefinitionName);
            System.out.println(beanDefinition);
        }

        //修改
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(BeanDefinitionBean.class);
        MutablePropertyValues properties = genericBeanDefinition.getPropertyValues();
        properties.add("name","Jack");
        registry.registerBeanDefinition("beanDefinitionBean",genericBeanDefinition);

        //增加扫描路径
        ClassPathBeanDefinitionScanner scanner= new ClassPathBeanDefinitionScanner(registry);
        scanner.addIncludeFilter(new AnnotationTypeFilter(MyServer.class));
        scanner.scan("com/zs/customBean");

        //删除bd
        registry.removeBeanDefinition("beanDefinitionBean");
        System.out.println(registry);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        DefaultListableBeanFactory beanFactory1 = (DefaultListableBeanFactory) beanFactory;
        //bean名称重复时覆盖
        beanFactory1.setAllowBeanDefinitionOverriding(true);
        beanFactory1.setAllowCircularReferences(true);

        BeanDefinitionRegistry registry = (BeanDefinitionRegistry)beanFactory;
        String[] beanDefinitionNames = registry.getBeanDefinitionNames();
        System.out.println(beanDefinitionNames.length);


    }

    @Override
    public int getOrder() {
        return 0;
    }
}
